package com.example.photography.adapter.product;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import com.example.photography.R;
import com.example.photography.adapter.MyBaseAdapter;
import com.example.photography.bean.MyContestBean;
import com.example.photography.util.Util;
import com.facebook.drawee.view.SimpleDraweeView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyContestAdapter extends MyBaseAdapter {

    public MyContestAdapter(List list, Context context) {
        super(list, context);
    }

    @Override
    public View myGetView(int position, View convertView) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_contest, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //根据位置装载数据
        MyContestBean.DataBean.ContestBean contestBean = (MyContestBean.DataBean.ContestBean) list.get(position);
        viewHolder.tvTheme.setText(contestBean.getTheme());
        viewHolder.tvTheme.setTextColor(Color.parseColor(contestBean.getThemecolor()));
        Date todate  = new Date(System.currentTimeMillis());
        Date endate = new Date();
        Date publishdate = new Date();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            endate = sd.parse(contestBean.getEnd_date());
            publishdate = sd.parse(contestBean.getPublish_date());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        viewHolder.tvDetails.setText("已投稿"+contestBean.getNums()+"，"+Util.getGapCount(todate,endate));
        viewHolder.svCover.setImageURI(contestBean.getCover());
        viewHolder.tvCommentNum.setText(contestBean.getContent_num()+"");
        viewHolder.tvLikeNum.setText(contestBean.getLike_num()+"");
        viewHolder.tvPublish.setText(Util.getTimeFormatText(publishdate));
        return convertView;
    }


    static
    class ViewHolder {
        @BindView(R.id.tv_theme)
        TextView tvTheme;
        @BindView(R.id.tv_details)
        TextView tvDetails;
        @BindView(R.id.sv_cover)
        SimpleDraweeView svCover;
        @BindView(R.id.tv_comment_num)
        TextView tvCommentNum;
        @BindView(R.id.tv_like_num)
        TextView tvLikeNum;
        @BindView(R.id.tv_publish)
        TextView tvPublish;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
